This is a complete, minimal example. If this class is packaged in a jar file with a correct manifest, when the VM starts up, the premain method gets called and has a chance to register an instance of a ClassFileTransformer with the instrumentation instance.
registrar
Here's a simple class that will report the unqualified class name and its package:
Below is the source for the registrar. Here are a couple of notes:
Many of the output strings are exposed as package-level constants to make testing easier
There's a static filed, ERROR_OUT, exposed for testability as well
This class records the each class it instantiates in registeredTransformers, again for testability
This class allows a : separated list of classes. E.g. rather than using multiple -javaagent: lines or passing parameters into the premain(...), I've choses to support a : separated list.
If you want to see the tests that verify this class' functionality, look further below. Note that the test class uses JUnit 4.4 and JMock 2.4.
Background
The only requirement for a class used to register a Java Agent is a premain method:This is a complete, minimal example. If this class is packaged in a jar file with a correct manifest, when the VM starts up, the premain method gets called and has a chance to register an instance of a ClassFileTransformer with the instrumentation instance.
registrar
Here's a simple class that will report the unqualified class name and its package:
To get this ClassFileTransformer registered, we'd change pre-main to the following:
ConfigurableClassFileTransformerRegistrar
Below is the source for the registrar. Here are a couple of notes:ConfigurableClassFileTransformerRegistrarTest